﻿(function ($) {
    $.fn.remotePanel = function (option) {
        option = $.extend({
            url: null,
            name: null,
            postdata: null,
            application: null,
            onload: function () { }
        }, option);
        return this.each(function () {
            var view = $(this);
            var settings = $.extend({}, option);
            var helper = new $.remotePanelHelper(view, settings);
            helper.init();
            view.helper("remotePanel", helper);
        });
    };
    $.fn.PanelParent = function () {
        return this.closest("[panelid]");
    };

    $.remotePanelHelper = function (jo, setting) {
        this.jo = jo;
        this.setting = setting;

        this.id = null;
        var _this = this;
        this.init = function () {
            this.id = "remotepanel" + new Date().getTime();
            this.setting.data = this.setting.postdata;
            this.setting.type = "html";
            this.setting.backtype = "fill";
            this.setting.fill = this.jo;
            this.setting.mask = true;
            this.setting.successmsg = null;
            this.setting.aftersuccess = this.setting.onload;

            this.jo.attr("panelid", this.id);
            this.jo.remote(function () {

                _this.setting.application.getAjaxManager().ajaxpost(_this.setting);

                //                if (_this.setting.postdata == null || _this.setting.postdata == "") {
                //                    _this.jo.load(_this.setting.url, function () {
                //                        _this.jo.renderui();
                //                        _this.setting.onload();
                //                    });
                //                } else {
                //                    $.post(_this.setting.url, $.param(_this.setting.postdata), function (html) {
                //                        _this.jo.html(html);
                //                        _this.jo.renderui();
                //                        _this.setting.onload();
                //                    });
                //                }
            });
            this.jo.remote();
        };
        this.getId = function () {
            return this.id;
        };
        this.getName = function () {
            return this.setting.name;
        };
        this.reload = function () {
           
            jo.remote();
        };
        //this.unRegister = function () {
        //    if (this.setting.application) {
        //        this.setting.application.removeRemotePanel(this);
        //    }
        //}
    };

})(jQuery);